package com.rankeiot.platform.inner.view;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Directive;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.jfinal.template.source.ISourceFactory;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class JFinalViewResolver extends AbstractTemplateViewResolver {
    public static final Engine engine = new Engine();
    static List<String> sharedFunctionFiles = new ArrayList();
    static boolean sessionInView = false;
    static boolean createSession = true;
    private static JFinalViewResolver me = null;

    public static JFinalViewResolver me() {
        return me;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setDevMode(boolean devMode) {
        engine.setDevMode(devMode);
    }

    public void setSharedFunction(String sharedFunctionFiles) {
        if (StrKit.isBlank(sharedFunctionFiles)) {
            throw new IllegalArgumentException("sharedFunctionFiles can not be blank");
        } else {
            String[] fileArray = sharedFunctionFiles.split(",");
            String[] var3 = fileArray;
            int var4 = fileArray.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String fileName = var3[var5];
                JFinalViewResolver.sharedFunctionFiles.add(fileName);
            }

        }
    }

    public void setSharedFunctionList(List<String> sharedFunctionList) {
        if (sharedFunctionList != null) {
            sharedFunctionFiles.addAll(sharedFunctionList);
        }

    }

    public void addSharedFunction(String fileName) {
        sharedFunctionFiles.add(fileName);
    }

    public void addDirective(String directiveName, Class<? extends Directive> directiveClass) {
        engine.addDirective(directiveName, directiveClass);
    }

    /** @deprecated */
    @Deprecated
    public void addDirective(String directiveName, Directive directive) {
        this.addDirective(directiveName, directive.getClass());
    }

    public void addSharedObject(String name, Object object) {
        engine.addSharedObject(name, object);
    }

    public void addSharedMethod(Object sharedMethodFromObject) {
        engine.addSharedMethod(sharedMethodFromObject);
    }

    public void addSharedMethod(Class<?> sharedMethodFromClass) {
        engine.addSharedMethod(sharedMethodFromClass);
    }

    public static void addExtensionMethod(Class<?> targetClass, Object objectOfExtensionClass) {
        Engine.addExtensionMethod(targetClass, objectOfExtensionClass);
    }

    public static void addExtensionMethod(Class<?> targetClass, Class<?> extensionClass) {
        Engine.addExtensionMethod(targetClass, extensionClass);
    }

    public void setSourceFactory(ISourceFactory sourceFactory) {
        if (sourceFactory instanceof ClassPathSourceFactory) {
            engine.setBaseTemplatePath((String)null);
        }

        engine.setSourceFactory(sourceFactory);
    }

    public void setToClassPathSourceFactory() {
        engine.setToClassPathSourceFactory();
    }

    public void setBaseTemplatePath(String baseTemplatePath) {
        engine.setBaseTemplatePath(baseTemplatePath);
    }

    public void setSessionInView(boolean sessionInView) {
        JFinalViewResolver.sessionInView = sessionInView;
    }

    public void setCreateSession(boolean createSession) {
       JFinalViewResolver.createSession = createSession;
    }

    public void setEncoding(String encoding) {
        engine.setEncoding(encoding);
    }

    public void setDatePattern(String datePattern) {
        engine.setDatePattern(datePattern);
    }

    public JFinalViewResolver() {
        Class var1 = JFinalViewResolver.class;
        synchronized(JFinalViewResolver.class) {
            if (me == null) {
                me = this;
            }
        }

        this.setViewClass(this.requiredViewClass());
        this.setOrder(0);
        this.setContentType("text/html;charset=UTF-8");
    }

    protected Class<?> requiredViewClass() {
        return JFinalView.class;
    }

    protected View loadView(String viewName, Locale locale) throws Exception {
        String suffix = this.getSuffix();
        return !".jsp".equals(suffix) && !".ftl".equals(suffix) && !".vm".equals(suffix) ? super.loadView(viewName, locale) : null;
    }


    private void initSharedFunction() {
        Iterator var1 = sharedFunctionFiles.iterator();

        while(var1.hasNext()) {
            String file = (String)var1.next();
            engine.addSharedFunction(file.trim());
        }

    }
}
